从零搭建微服务

您所在的位置:网站首页 从零开始的spring security oauth2一 程序 从零搭建微服务

从零搭建微服务

2023-06-01 02:54| 来源: 网络整理| 查看: 265

写在最前

如果这个项目让你有所收获,记得 Star 关注哦,这对我是非常不错的鼓励与支持。

源码地址:https://gitee.com/csps/mingyue

文档地址:https://gitee.com/csps/mingyue/wikis

目录规划

截止目前,项目可以规划如下:

mingyue ├── mingyue-auth -- 认证中心[9000] └── mingyue-common -- 公共模块 ├── mingyue-common-bom -- 全局依赖管理控制 ├── mingyue-common-core -- 公共工具类核心包 ├── mingyue-common-satoken -- Sa-Token └── mingyue-common-security -- 认证工具类 什么是 BOM?

BOM全称是Bill Of Materials,译作材料清单。BOM本身并不是一种特殊的文件格式,而是一个普通的POM文件,只是在这个POM中,我们罗列的是一个工程的所有依赖和其对应的版本。该文件一般被其它工程使用,当其它工程引用BOM中罗列的jar包时,不用显示指定具体的版本,会自动使用BOM对应的jar版本。

所以BOM的好处是用来管理一个工程的所有依赖版本信息。

结构优化 创建 mingyue-common

Idea mingyue 右击 -> New -> Module -> mingyue-common

只保留 pom.xml 其余文件删除

mingyue-common pom mingyue 公共聚合模块 mingyue-common-bom mingyue-common-security mingyue-common-satoken 创建 mingyue-common-satoken mingyue-common-satoken cn.dev33 sa-token-spring-boot-starter ${sa-token.version} cn.dev33 sa-token-oauth2 ${sa-token.version} cn.dev33 sa-token-dao-redis-jackson ${sa-token.version} org.apache.commons commons-pool2 org.springframework.boot spring-boot-starter-thymeleaf 创建 mingyue-common-security mingyue-common-security cn.dev33 sa-token-spring-boot-starter ${sa-token.version} cn.dev33 sa-token-oauth2 ${sa-token.version} cn.dev33 sa-token-dao-redis-jackson ${sa-token.version} org.apache.commons commons-pool2 org.springframework.boot spring-boot-starter-thymeleaf 创建 mingyue-common-bom

只保留 pom.xml 其余文件删除

mingyue-common-bom pom mingyue-common-bom common 依赖项 com.csp.mingyue mingyue-common-security ${project.version} com.csp.mingyue mingyue-common-satoken ${project.version} 引入依赖 mingyue 引入 mingyue-common-bom ... com.csp.mingyue mingyue-common-bom ${mingyue.version} pom import 启动 mingyue-auth 测试

参考【认证中心(二)】测试接口

全局异常处理 /** * 全局异常处理器 * * @author ruoyi */ @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { /** * Sa-Token 框架内部逻辑发生错误抛出的异常 */ @ExceptionHandler(SaTokenException.class) public R handlerException(SaTokenException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',Sa-Token 框架内部逻辑发生错误抛出的异常 '{}'", requestURI, e.getMessage()); return R.fail(e.getMessage()); } /** * 认证失败 */ @ExceptionHandler(NotLoginException.class) public R handleNotLoginException(NotLoginException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); return R.fail(HttpStatus.UNAUTHORIZED, "认证失败,无法访问系统资源"); } /** * 请求方式不支持 */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); return R.fail(e.getMessage()); } /** * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) public R handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); return R.fail(e.getMessage()); } /** * 系统异常 */ @ExceptionHandler(Exception.class) public R handleException(Exception e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生系统异常.", requestURI, e); return R.fail(e.getMessage()); } /** * 自定义验证异常 */ @ExceptionHandler(BindException.class) public R handleBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return R.fail(message); } /** * 自定义验证异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); return R.fail(message); } } 小结

优化了一下结构,下一节预计接入前端框架(前后端分离模式),实现登录与登出



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3